/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.repository.mapper.standard;

import cn.ac.nercis.pes.model.dto.evaluation.asset.*;
import cn.ac.nercis.pes.model.standard.SystemEvaluationSDTO;
import cn.ac.nercis.pes.model.standard.SystemSDTO;
import cn.ac.nercis.pes.model.standard.asset.AssetAttributeSDTO;
import cn.ac.nercis.pes.model.standard.asset.AssetSDTO;
import cn.ac.nercis.pes.repository.model.evaluation.AssetAttributeDO;
import cn.ac.nercis.pes.repository.model.evaluation.AssetDO;
import cn.ac.nercis.pes.repository.model.project.SystemDO;
import cn.ac.nercis.pes.repository.model.project.SystemEvaluationDO;
import lombok.NonNull;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Builder;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;

import java.util.Optional;

/**
 * 系统数据导入相关信息类型映射
 *
 * @author zpy
 * @version 1.0.0
 */
@Mapper(componentModel = "spring",
        imports = {
                Optional.class,
                AssetDO.class,
                AssetAttributeDO.class
        },
        unmappedTargetPolicy = ReportingPolicy.IGNORE,
        builder = @Builder(disableBuilder = true))
public interface SystemImportMapper {
    SystemImportMapper INSTANCE = Mappers.getMapper(SystemImportMapper.class);

    SystemDO toDO(SystemSDTO dto);

    SystemEvaluationDO toDO(SystemEvaluationSDTO dto);

    AssetDO toDO(AssetSDTO dto);

    AssetAttributeDO toDO(AssetAttributeSDTO dto);

    default PhyEnvDTO toPhyEnvDTO(@NonNull AssetSDTO assetDO) {
        PhyEnvDTO data = PhyEnvDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if (CollectionUtils.isNotEmpty(assetDO.getAttributes())) {
            for (AssetAttributeSDTO att : assetDO.getAttributes()) {
                data.setField(att.getFieldName(), att.getValue());
            }
        }
        return data;
    }

    default PhySecurityDTO toPhySecurity(@NonNull AssetSDTO assetDO) {
        PhySecurityDTO data = PhySecurityDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if (CollectionUtils.isNotEmpty(assetDO.getAttributes())) {
            for (AssetAttributeSDTO att : assetDO.getAttributes()) {
                data.setField(att.getFieldName(), att.getValue());
            }
        }
        return data;
    }

    default PwdProductDTO toPwdProduct(@NonNull AssetSDTO assetDO) {
        PwdProductDTO data = PwdProductDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if (CollectionUtils.isNotEmpty(assetDO.getAttributes())) {
            for (AssetAttributeSDTO att : assetDO.getAttributes()) {
                data.setField(att.getFieldName(), att.getValue());
            }
        }
        return data;
    }

    default ServerStorageDTO toServerStorage(@NonNull AssetSDTO assetDO) {
        ServerStorageDTO data = ServerStorageDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if (CollectionUtils.isNotEmpty(assetDO.getAttributes())) {
            for (AssetAttributeSDTO att : assetDO.getAttributes()) {
                data.setField(att.getFieldName(), att.getValue());
            }
        }
        return data;
    }

    default NetworkSecurityDTO toNetworkSecurity(@NonNull AssetSDTO assetDO) {
        NetworkSecurityDTO data = NetworkSecurityDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if (CollectionUtils.isNotEmpty(assetDO.getAttributes())) {
            for (AssetAttributeSDTO att : assetDO.getAttributes()) {
                data.setField(att.getFieldName(), att.getValue());
            }
        }
        return data;
    }

    default DatabaseDTO toDatabase(@NonNull AssetSDTO assetDO) {
        DatabaseDTO data = DatabaseDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if (CollectionUtils.isNotEmpty(assetDO.getAttributes())) {
            for (AssetAttributeSDTO att : assetDO.getAttributes()) {
                data.setField(att.getFieldName(), att.getValue());
            }
        }
        return data;
    }

    default KeyBusinessDTO toKeyBusiness(@NonNull AssetSDTO assetDO) {
        KeyBusinessDTO data = KeyBusinessDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if (CollectionUtils.isNotEmpty(assetDO.getAttributes())) {
            for (AssetAttributeSDTO att : assetDO.getAttributes()) {
                data.setField(att.getFieldName(), att.getValue());
            }
        }
        return data;
    }

    default SubBusinessDTO toSubBusiness(@NonNull AssetSDTO assetDO) {
        SubBusinessDTO data = SubBusinessDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if (CollectionUtils.isNotEmpty(assetDO.getAttributes())) {
            for (AssetAttributeSDTO att : assetDO.getAttributes()) {
                data.setField(att.getFieldName(), att.getValue());
            }
        }
        return data;
    }

    default KeyDataDTO toKeyData(@NonNull AssetSDTO assetDO) {
        KeyDataDTO data = KeyDataDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .keyDataAssetId(assetDO.getKeyDataAssetId())
                .build();
        if (CollectionUtils.isNotEmpty(assetDO.getAttributes())) {
            for (AssetAttributeSDTO att : assetDO.getAttributes()) {
                data.setField(att.getFieldName(), att.getValue());
            }
        }
        return data;
    }

    default ChannelDTO toChannel(@NonNull AssetSDTO assetDO) {
        ChannelDTO data = ChannelDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if (CollectionUtils.isNotEmpty(assetDO.getAttributes())) {
            for (AssetAttributeSDTO att : assetDO.getAttributes()) {
                data.setField(att.getFieldName(), att.getValue());
            }
        }
        return data;
    }

    default ManageDocumentDTO toManageDocument(@NonNull AssetSDTO assetDO) {
        ManageDocumentDTO data = ManageDocumentDTO.builder()
                .id(assetDO.getId())
                .isUse(assetDO.getIsUse())
                .desc(assetDO.getDesc())
                .quoteAssetId(assetDO.getQuoteAssetId())
                .build();
        if (CollectionUtils.isNotEmpty(assetDO.getAttributes())) {
            for (AssetAttributeSDTO att : assetDO.getAttributes()) {
                data.setField(att.getFieldName(), att.getValue());
            }
        }
        return data;
    }
}